import random, string
from tasks import async_redis

import asyncio, json


def rangdom_str(length: int):
    seed = string.ascii_letters + string.digits
    return ''.join(random.choices(seed, k=length)).lower()


def get_loop_redis():
    loop = asyncio.get_event_loop()
    if not loop:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop=loop)
    return loop, loop.run_until_complete(async_redis())


def friend_dumps_json(data: dict):
    if not isinstance(data, (dict, list)):
        return dict()
    return json.dumps(data)


def friend_loads_json(data: str):
    return json.loads(data)
